<?php

namespace App\Http\Controllers\admin\login;

use App\Http\Controllers\Controller;
use App\Logic\LoginLogic;
use App\Models\Login;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use phpseclib3\File\ASN1\Maps\PostalAddress;

class LoginController extends Controller
{

    /**
     * @后台登录展示页面
     * @user 刘璇
     * @method get
     * @url https://www.healthcare.com/login
     * @data
     * @return \Illuminate\Http\Response
     */
    public function loginAdmin()
    {
        return view('Login/login');
    }
    /**
     * @后台登录
     * @user 刘璇
     * @method Post
     * @url https://www.healthcare.com/dologin
     * @data
     * @return \Illuminate\Http\Response
     */
    public function dologinAdmin(Request $request){
        $data = $request->all();
        //表单进行验证
        $validated = $request->validate([
            'password' => 'required',//密码不能为空
            'phone' => 'required|regex:/^1[3-9]\d{9}$/',
            //手机号不能为空 手机号以数字 1开头，第二位为 3~9的数字，随后9位为数字组成的11位手机号
        ], [
            'password.required' => '密码不能为空',
            'phone.required' => '手机号码不能为空',
            'phone.regex' => '手机号码格式不正确',
        ]);
        try {
            //登录时通过手机号判断用户是否存在
            $user = Login::with(['role','role.roleNode'])->where('phone', $data['phone'])->first()->toArray();
            $res = LoginLogic::dologin($data,$user);
            return redirect("/user/back_admin");
        } catch (\Exception $exception) {
            //捕获异常
            Log::info($exception->getMessage());
        }


    }
    /**
     * @后台退出登录
     * @user 刘璇
     * @method get
     * @url https://www.healthcare.com/loginout
     * @data
     * @return \Illuminate\Http\Response
     */
    public function loginOut(){
        session('id',null);
        return redirect('login');
    }


}
